<?php

class Admin_BrandController extends Zend_Controller_Action {

    public function init() {
        if (!Zend_Auth::getInstance()->hasIdentity()) {
            $this->_redirect("/admin/login");
        }
        $this->_helper->layout->setLayout("admin");
    }

    public function indexAction() {
        $db = new Admin_Model_Brand();
        $fetchAll = $db->fetchAll();
        $this->view->allBrands = $fetchAll;
    }

    public function addAction() {
        $form = new Admin_Form_BrandAdd();
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $adapter = $form->photo->getTransferAdapter();
                $photoName = "";
                foreach ($adapter->getFileInfo() as $info) {
                    $extension = pathinfo($info['name'], PATHINFO_EXTENSION);
                    $basename = basename($info['name'], '.' . $extension);
                    $newName = md5($basename) . '_' . md5(time()) . '.' . $extension;
                    $nName = iconv('utf-8', 'cp1251', $newName);
                    $photoName = $newName;
                    $adapter->addFilter('Rename', array(
                        'target' => $nName,
//                        'source' => $info['tmp_name'],
                        'overwrite' => true
                    ));
                    $adapter->receive($info['name']);
                }
                $db = new Admin_Model_Brand();
                $db->addNewBrand(array(
                    'name' => $values['name'],
                    'image' => $photoName,
                    'menu_id' => $values['menu'],
                    'enabled' => $values['enabled']
                ));
                $this->_redirect("/admin/brand");
            }
        }
    }

    public function updateAction() {
        $id = $this->getinputidAction();
        $brandDb = new Admin_Model_Brand();
        $brandById = $brandDb->getBrandById($id);
        $form = new Admin_Form_BrandUpdate();
        $form->main($brandById[0]);
        $this->view->form = $form;
        if ($this->getRequest()->isPost()) {
            if ($form->isValid($this->getRequest()->getPost())) {
                $values = $form->getValues();
                $data = array(
                    'name' => $values['name'],
                    'menu_id' => $values['menu'],
                    'enabled' => $values['enabled']
                );
                if ($values['photo'] != null) {
                    $adapter = $form->photo->getTransferAdapter();
                    $photoName = "";
                    foreach ($adapter->getFileInfo() as $info) {
                        $extension = pathinfo($info['name'], PATHINFO_EXTENSION);
                        $basename = basename($info['name'], '.' . $extension);
                        $newName = md5($basename) . '_' . md5(time()) . '.' . $extension;
                        $nName = iconv('utf-8', 'cp1251', $newName);
                        $photoName = $newName;
                        $adapter->addFilter('Rename', array(
                            'target' => $nName,
//                        'source' => $info['tmp_name'],
                            'overwrite' => true
                        ));
                        $adapter->receive($info['name']);
                    }
                    $data['image'] = $photoName;
                }

                $db = new Admin_Model_Brand();
                $db->updateBrand($data, $values['id']);
                $this->_redirect("/admin/brand");
            }
        }
    }

    /**
     * Get ID.    
     */
    public function getinputidAction() {
        $filters = array(
            'id' => array('HtmlEntities', 'StripTags', 'StringTrim')
        );
        $validators = array(
            'id' => array('Int')
        );
        $input = new Zend_Filter_Input($filters, $validators);
        $input->setData($this->getRequest()->getParams());
        $id = $input->id; // id
        return $id;
    }

}
